<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\ExampleDownload;
use app\common\model\Example;
use app\common\model\Picture;
use app\common\model\PictureDownload;

class Down extends BaseController
{
    //素材库下载图片
    public function down()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(422, '请传入id');
        }
        $image = Picture::where(['status' => 1, 'id' => $id])
            ->field('id,image')
            ->find();
        $image['image'] = env('app.yuming') . $image['image'];
        $down = new PictureDownload();
        $picture = Picture::where(['status' => 1, 'id' => $id])
            ->value('id');
        if (empty($picture)) {
            return errMsg(400, '数据不存在');
        }
        $data = [];
        $data['image'] = str_replace(env('app.yuming'), '', $image['image']);
        $data['picture_id'] = $id;
        $data['member_id'] = $this->user_id;
        if ($down->save($data)) {
            return sucessMsg(200, '请求成功', $image['image']);
        } else {
            return errMsg(400, '失败');
        }
    }

    //写入实例下载记录表
    public function example_down()
    {
        $example_id = input('id/d');
        if (empty($example_id)) {
            return errMsg(422, '请选择id');
        }
        $example = Example::where(['status' => 1, 'id' => $example_id])
            ->field('id,title')
            ->find();
        if (empty($example)) {
            return errMsg(400, '数据不存在');
        }
        $data = [];
        $data['title'] = $example['title'];
        $data['example_id'] = $example_id;
        $data['member_id'] = $this->user_id;
        if (ExampleDownload::save($data)) {
            return sucessMsg(200, '成功');
        } else {
            return errMsg(400, '失败');
        }
    }


    /**
     * 显示我的下载-实例
     */
    public function example()
    {
        $page = input('page/d', 1);
        $listRows = 4;
        $user_id = $this->user_id;
        $example = ExampleDownload::where('member_id', $user_id)
            ->field('id,example_id,title,created_at')
            ->page($page, $listRows)
            ->order('created_at', 'desc')
            ->select();
        if (empty($example)) {
            return errMsg(422, '暂无数据');
        }
        $count = ExampleDownload::where('member_id', $user_id)->count();
        $maxpage = ceil($count / $listRows);
        return sucessMsg(200, '请求成功', ['example' => $example, 'page' => $page, 'maxpage' => $maxpage]);
    }

    /**
     * 显示我的下载-素材
     */
    public function sucai()
    {
        $page = input('page/d', 1);
        $listRows = 8;
        $user_id = $this->user_id;
        $picture = PictureDownload::where('member_id', $user_id)
            ->field('id,picture_id,image')
            ->withAttr('image', function ($val, $data) {
                return env('app.yuming') . $val;
            })
            ->page($page, $listRows)
            ->order('created_at', 'desc')
            ->select();
        if (empty($picture)) {
            return errMsg(422, '暂无数据');
        }
        $count = PictureDownload::where('member_id', $user_id)->count();
        $maxpage = ceil($count / $listRows);
        return sucessMsg(200, '请求成功', ['picture' => $picture, 'page' => $page, 'maxpage' => $maxpage]);
    }

    /**
     * 我的下载-素材删除
     */
    public function example_del()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(422, '请传入id');
        }
        if (ExampleDownload::destroy($id)) {
            return sucessMsg(200, '删除成功');
        }
        return errMsg(400, '删除失败');
    }

    /**
     * 我的下载-素材删除
     */
    public function sucai_del()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(422, '请传入id');
        }
        if (PictureDownload::destroy($id)) {
            return sucessMsg(200, '删除成功');
        }
        return errMsg(400, '删除失败');
    }
}
